package com.yanqu.road.server.manger.maidian.patrons;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.patrons.PatronsStage;
import com.yanqu.road.entity.config.promotion.PromotionInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.enums.eSkinType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.PromotionMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsLog {

    public static void trackPatronsInfoSnapshot(GamePlayer player) {
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            //公共事件属性
            DataBuilder common = DataBuilder.newBuilder(player.getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
            DataAnalyticsMgr.buildPublicEventData(player, common);

            PatronsModule patronsModule = player.getModule(PatronsModule.class);
            List<UserPatrons> patronsList = patronsModule.getUserPatronsList();
            patronsList.sort((o1, o2) -> o2.getAbility().compareTo(o1.getAbility()));
            int i = 0;
            for (UserPatrons userPatrons : patronsList) {
                DataBuilder dataBuilder = DataBuilder.newBuilder(player.getUserInfo().getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
                PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());

                dataBuilder.build(EventDataConstants.Patrons.GUEST_ID, userPatrons.getPatronsId());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_QUELITY, patronsInfo.getQuality());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_VOCATION, patronsInfo.getOccupation());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_LEVEL, userPatrons.getLevel());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_INTELLIGENCE, userPatrons.getTotalQualification());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_POWER, userPatrons.getAbility().longValue());
                dataBuilder.build(EventDataConstants.Patrons.GUEST_RANK, ++i);
                if(userPatrons.getShowPromotionId() > 0){
                    PromotionInfo promotionInfo = PromotionMgr.getPromotionInfoById(userPatrons.getShowPromotionId());
                    if(promotionInfo != null){
                        dataBuilder.build(EventDataConstants.Patrons.GUEST_QUELITY, promotionInfo.getQuality());
                    }
                }
                //服装技能
                List<Integer> skinSkillList = new ArrayList<>();
                //服装
                List<UserSkin> userSkinList = player.getModule(SkinModule.class).getPatronsSkinList();
                List<Map<String, Object>> objectGroup = new ArrayList<>();
                for (UserSkin userSkin : userSkinList) {
                    SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
                    if(skinInfo == null){
                        continue;
                    }
                    if(skinInfo.getBelongRole() != userPatrons.getPatronsId()){
                        continue;
                    }
                    skinSkillList.addAll(skinInfo.getSkinEffect());
                    Map<String, Object> objectMap = new HashMap<>();
                    objectMap.put(EventDataConstants.Patrons.GUEST_CLOTHING_ID, userSkin.getSkinId());
                    objectMap.put(EventDataConstants.Patrons.GUEST_CLOTHING_LV, userSkin.getLevel());
                    objectGroup.add(objectMap);
                }
                dataBuilder.build(EventDataConstants.Patrons.GUEST_CLOTHING, objectGroup);

                //天赋精进星级
                dataBuilder.build(EventDataConstants.Patrons.GUEST_TALENTSTAR, userPatrons.getStageLv());

                //基础技能
                List<Map<String, Object>> baseSkillGroup = new ArrayList<>();
                //副业技能
                //List<Map<String, Object>> sidesSkillGroup = new ArrayList<>();
                //光环技能
                //List<Map<String, Object>> haloSkillGroup = new ArrayList<>();

                //天赋技能
                List<Map<String, Object>> talentSkillGroup = new ArrayList<>();
                //精进技能
                List<Map<String, Object>> jingSkillGroup = new ArrayList<>();
                //授学技能
                List<Map<String, Object>> teachSkillGroup = new ArrayList<>();
                //服装技能
                List<Map<String, Object>> skinSkillGroup = new ArrayList<>();

                for (UserPatronsSkill patronsSkill : userPatrons.getSkillList()) {
                    int skillId = patronsSkill.getSkillId();
                    SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                    if(skillInfo == null){
                        continue;
                    }
                    int maxLevel = patronsModule.getMaxLevel(patronsSkill, skillInfo, patronsSkill.getType(), userPatrons.getPatronsId());
                    //天赋技能集合
                    Set<Integer> talentSkillList = new HashSet<>(patronsInfo.getTalentList());
                    if(userPatrons.getShowPromotionId() > 0) {
                        PromotionInfo promotionInfo = PromotionMgr.getPromotionInfoById(userPatrons.getShowPromotionId());
                        if(promotionInfo != null){
                            talentSkillList.addAll(promotionInfo.getTalentList());
                        }
                    }
                    if(talentSkillList.contains(skillId)){
                        //天赋
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventDataConstants.Patrons.GUEST_TALENTSKILLNAME, skillId);
                        map.put(EventDataConstants.Patrons.GUEST_TALENTSKILLMAX, maxLevel);
                        map.put(EventDataConstants.Patrons.GUEST_TALENTSKILLLV, patronsSkill.getSkillLv());
                        talentSkillGroup.add(map);
                        continue;
                    }
                    //授学技能
                    if(skillInfo.getUpgradeParamList().size() == 2 && skillInfo.getUpgradeParamList().get(0) == 104 && skillInfo.getUpgradeParamList().get(1) == 65){
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventDataConstants.Patrons.GUEST_TEACHSKILLNAME, skillId);
                        map.put(EventDataConstants.Patrons.GUEST_TEACHSKILLMAX, maxLevel);
                        map.put(EventDataConstants.Patrons.GUEST_TEACHSKILLLV, patronsSkill.getSkillLv());
                        teachSkillGroup.add(map);
                        continue;
                    }

                    //精进技能
                    talentSkillList.clear();
                    for (Map.Entry<Integer, PatronsStage> entry : PatronsMgr.getPatronsStageMap().entrySet()) {
                        PatronsStage patronsStage = entry.getValue();
                        if(patronsStage.getAddSkillId() > 0){
                            talentSkillList.add(patronsStage.getAddSkillId());
                        }
                    }
                    if(talentSkillList.contains(skillId)){
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventDataConstants.Patrons.GUEST_JINGJINSKILLNAME, skillId);
                        map.put(EventDataConstants.Patrons.GUEST_JINGJINSKILLMAX, maxLevel);
                        map.put(EventDataConstants.Patrons.GUEST_JINGJINSKILLLV, patronsSkill.getSkillLv());
                        jingSkillGroup.add(map);
                        continue;
                    }

                    //服装
                    if(skinSkillList.contains(skillId)){
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventDataConstants.Patrons.GUEST_CLOTHINGSKILLNAME, skillId);
                        map.put(EventDataConstants.Patrons.GUEST_CLOTHINGSKILLMAX, maxLevel);
                        map.put(EventDataConstants.Patrons.GUEST_CLOTHINGSKILLLV, patronsSkill.getSkillLv());
                        skinSkillGroup.add(map);
                        continue;
                    }

                    //光环
                    //基础
                    //副业
                    Map<String, Object> map = new HashMap<>();
                    map.put(EventDataConstants.Patrons.GUEST_SKILLNAME, skillId);
                    map.put(EventDataConstants.Patrons.GUEST_SKILLMAX, maxLevel);
                    map.put(EventDataConstants.Patrons.GUEST_SKILLLV, patronsSkill.getSkillLv());
                    baseSkillGroup.add(map);

                }

                dataBuilder.build(EventDataConstants.Patrons.GUEST_TALENTSKILL, talentSkillGroup);
                dataBuilder.build(EventDataConstants.Patrons.GUEST_SKILL, baseSkillGroup);
                dataBuilder.build(EventDataConstants.Patrons.GUEST_CLOTHINGSKILL, skinSkillGroup);
//                dataBuilder.build(EventDataConstants.Patrons.GUEST_HALOSKILL, haloSkillGroup);
                dataBuilder.build(EventDataConstants.Patrons.GUEST_JINGJINSKILL, jingSkillGroup);
                //dataBuilder.build(EventDataConstants.Patrons.GUEST_TEACHSKILL, teachSkillGroup);
//                dataBuilder.build(EventDataConstants.Patrons.GUEST_SIDESKILL, sidesSkillGroup);

                dataBuilder.build("guest_base_intelligence", userPatrons.getQualification());
                dataBuilder.build("guest_base_power", userPatrons.getBaseAbility().toString());

                //授学等级
                UserPatronsSkill teacherSkill = userPatrons.getPatronsSkill(ePatronsSkillType.Normal.getValue(), GameConfig.GRAND_CHILD_TEACH_SKILL_ID);
                if(teacherSkill != null) {
                    dataBuilder.build(EventDataConstants.Patrons.CURRENT_TEACHER_LEVEL, teacherSkill.getSkillLv());
                }

                dataBuilder.getDataMap().putAll(common.getDataMap());
                //直接上报，不再缓存
                DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), EventDataConstants.Patrons.GUEST_INFO, dataBuilder.getDataMap());
            }



        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }
    }
}
